﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using System.Text.RegularExpressions;
namespace BIZ
{
    public class StringTool
    {
        #region md5
        public static string md5(string str, int code)
        {
            string strEncrypt = string.Empty;
            if (code == 16)
            {
                strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").Substring(8, 16);
            }

            if (code == 32)
            {
                strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
            }

            return strEncrypt;
        }
        #endregion

        #region Substring
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="startString">开始字符串</param>
        /// <param name="endString">结束字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string Substring(string source, string startString, string endString)
        {
            string temp = source;
            if (startString != null && startString != string.Empty)
            {
                int start = temp.IndexOf(startString);
                if (start > -1)
                {
                    temp = temp.Substring(start, startString.Length);
                }
            }
            if (endString != null && endString != string.Empty)
            {
                int end = temp.IndexOf(endString);
                if (end > -1)
                {
                    temp = temp.Substring(0, end);
                }
            }
            return temp != source ? temp : string.Empty;
        }

        /// <summary>
        /// 截取字符串(字符串如果操过指定长度则将超出的部分用指定字符串代替)
        /// </summary>
        /// <param name="source">要检查的字符串</param>
        /// <param name="length">指定长度</param>
        /// <param name="tail">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string Substring(string source, int length, string tail)
        {
            return Substring(source, 0, length, tail);
        }


        /// <summary>
        /// 取指定长度的字符串
        /// </summary>
        /// <param name="source">要检查的字符串</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="length">指定长度</param>
        /// <param name="tail">用于替换的字符串</param>
        /// <returns>截取后的字符串</returns>
        public static string Substring(string source, int startIndex, int length, string tail)
        {
            string myResult = source;
            Byte[] bComments = Encoding.UTF8.GetBytes(source);
           
            if (length >= 0)
            {
                byte[] bsSrcString = Encoding.Default.GetBytes(source);
                //当字符串长度大于起始位置
                if (bsSrcString.Length > startIndex)
                {
                    int p_EndIndex = bsSrcString.Length;
                    //当要截取的长度在字符串的有效长度范围内
                    if (bsSrcString.Length > (startIndex + length))
                    {
                        p_EndIndex = length + startIndex;
                    }
                    else
                    {   //当不在有效范围内时,只取到字符串的结尾
                        length = bsSrcString.Length - startIndex;
                        tail = "";
                    }
                    int nRealLength = length;
                    int[] anResultFlag = new int[length];
                    byte[] bsResult = null;
                    int nFlag = 0;
                    for (int i = startIndex; i < p_EndIndex; i++)
                    {
                        if (bsSrcString[i] > 127)
                        {
                            nFlag++;
                            if (nFlag == 3)
                            {
                                nFlag = 1;
                            }
                        }
                        else
                        {
                            nFlag = 0;
                        }
                        anResultFlag[i] = nFlag;
                    }
                    if ((bsSrcString[p_EndIndex - 1] > 127) && (anResultFlag[length - 1] == 1))
                    {
                        nRealLength = length + 1;
                    }
                    bsResult = new byte[nRealLength];
                    Array.Copy(bsSrcString, startIndex, bsResult, 0, nRealLength);
                    myResult = Encoding.Default.GetString(bsResult);
                    myResult = myResult + tail;
                }
            }

            return myResult;
        }
        #endregion

        public static string changeImgUrl(string cUrl)
        {
            return System.Configuration.ConfigurationManager.AppSettings["imgserver"].ToString() + cUrl;
        }
        public static string changeImgUrlFromOtherServer(string cUrl)
        {
            return System.Configuration.ConfigurationManager.AppSettings["otherimgserver"].ToString() + cUrl.Replace("..","");
        }
        public static string GetImgs(string HTMLStr)
        {
            //string str = string.Empty;
            //Regex r = new Regex(@"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>", RegexOptions.Compiled);
            //Match m = r.Match(HTMLStr.ToLower());
            //if (m.Success)
            //    str = m.Result("${url}");
            //else
            //{
            //    str = ""; //否则显示默认图片
            //}
            //return str;
            Regex r = new Regex(@"<img[\s\S]*?>", RegexOptions.IgnoreCase);

            string txt = string.Empty;
            MatchCollection col = r.Matches(HTMLStr);
            foreach (Match item in col)
                HTMLStr = HTMLStr.Replace(GetImgurl(item.Value).Replace("\"", ""), BIZ.StringTool.changeImgUrlFromOtherServer(GetImgurl(item.Value).Replace("\"", "")));
            return HTMLStr;
        }
        public static string GetImgurl(string HTMLStr)
        {
            string str = string.Empty;
            Regex r = new Regex(@"<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>", RegexOptions.Compiled);
            Match m = r.Match(HTMLStr);
            if (m.Success)
                str = m.Result("${url}");
            else
            {
                str = ""; //否则显示默认图片
            }
            return str;
        }
    }
}
